package com.lerays.spider.sougou.handler;

import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.util.Iterator;
import java.util.List;

import org.lionsoul.websnail.json.JSONArray;
import org.lionsoul.websnail.json.JSONObject;

import com.lerays.spider.sougou.SpiderHttpServer;
import com.lerays.spider.sougou.hack.Session;
import com.lerays.spider.sougou.hack.SessionFactory;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;

/**
 * session list request handler.
 * 
 * @author chenxin<chenxin619315@gmail.com>
 */
public class SessionClearRequestHandler implements HttpHandler
{
	private SpiderHttpServer httpServer = null;
	
	public SessionClearRequestHandler( SpiderHttpServer httpServer )
	{
		this.setHttpServer(httpServer);
	}

	@Override
	public void handle(HttpExchange e) throws IOException 
	{
		SessionFactory sessionFactory = httpServer.getsougouSpider().getSessionFactory();
		sessionFactory.clear();
		
		String ret = "{\"status\":\"Ok\"}";
		e.sendResponseHeaders(200, ret.length());
        OutputStream os = e.getResponseBody();
        os.write(ret.getBytes());
        os.close();
        e.close();
	}

	public SpiderHttpServer getHttpServer() 
	{
		return httpServer;
	}

	public void setHttpServer(SpiderHttpServer httpServer) 
	{
		this.httpServer = httpServer;
	}
}
